import axios from "axios";
import router from "@/router";

const request=axios.create({
    baseURL:'http://localhost:8089',
    timeout:30000
})

//request拦截器
request.interceptors.request.use(config=>{
        config.headers['Content-Type'] = 'application/json;charset=UTF-8';
        let token = JSON.parse(localStorage.getItem("token")||'{}')
    console.log(token)
        config.headers['satoken']=token;//设置请求头
        return config;
    },error=>{
        console.log('request error'+error)
        return Promise.reject(error)
    });

//response
request.interceptors.response.use(
    response=>{
        let res = response.data;
        if(typeof res ==='string'){
            res = res ? JSON.parse(res) : res
        }
        if(res.code ==='401'){
            router.push('/login')
        }
        return res;
    },
    error => {
        console.log('request error'+error)
        return Promise.reject(error)
    }
)

export default request
